<?php
/**
 * 
 * 展商-联系人
 *  stj
 
 */
namespace app\api\controller\followmanage;
use app\api\controller\common\ApiBase;
use app\api\controller\common\CommonPlus; 
class Companyfollowlinkuser extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Companyfollowlinkuser');
    }

    /**
     * 人员列表 company_user
     * 前端调用
     */
    public function zslinkuserList(){
      
        $page = $this->request->param('page');
        $where=array();
        $page = max(1,intval($page));
        $pagesize = 10;


        $zsid = $this->request->param('zsid'); //首次就应该 只是展商id 来的
        // 查询 st
        $name = $this->request->param('name');
        $depart = $this->request->param('depart');
        $compname = $this->request->param('compname');

        $keyword="";
        if( !empty($name) ){
            $keyword = $name;
            $where['name_cn|surname_cn'] = array('like','%'.$keyword.'%'); 
        }

        if( !empty($depart) ){
            $keyword = $depart;
            $where['department_cn|department_en'] = array('like','%'.$keyword.'%'); 
        }
        if( !empty($compname) ){
            $keyword = $compname;
            $where['companyname'] = array('like','%'.$keyword.'%'); 
        }
        // 条件查询 ed    
        $where['zsid'] = $zsid; 
        $where['is_delete'] = 0; 
    
        if(!empty($keyword)){
            // $where['company_name|company_name_en'] = array('like','%'.$keyword.'%'); 
        }

        $list = $this->model->listWherePage($where,'id desc',($page-1)*$pagesize,$pagesize);
        $total = $this->model->countWhere($where);

        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total,'countpage'=>$total]]);
   
    }


     /**
     * 企业列表 内部调用 做筛选
     * 
     */
    public function zslinkuserListall(){
      
        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page');
        $where=array();
        $page = max(1,intval($page));
        $pagesize = 10;
  
       $where['is_delete'] = 0; //回头问下
      
        if(!empty($keyword)){
            $where['company_name|company_name_en'] = array('like','%'.$keyword.'%');
        }

        $list = $this->model->listAll($where,'id desc',($page-1)*$pagesize,$pagesize);
        // $total = $this->model->countWhere($where);
        $total=1;
        //return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total,'countpage'=>ceil($total/$pagesize)]]);
        return $list;
    }

    /**
     * 企业基本信息
     * @return \think\response\Json|void
     */
    public function zslinkuserDetail(){       
        // $zsid = $this->request->param('zsid');
        $id = $this->request->param('id'); // 明细 应该是 列表的id 
        
        // $where['zsid'] = $zsid;
        $where['id'] = $id;
        $where['is_delete'] = 0;
        $info = $this->model->findWhere($where);

        return json(['code'=>1,'msg'=>'success','data'=>$info]);
    }

    /**
     * 新增企业
     * @return \think\response\Json|void
     */
    public function addzslinkuser(){ 
        //==common
        $create_uid  = $this->admin['id'];
        $remark = $this->request->param('remark');        
        //== comon    
     
        $name = $this->request->param('name');     // 姓名 
        $zsid = $this->request->param('zsid');// 展商 id  
       // $companyid = $this->request->param('companyid');// 企业id  
        $sex = $this->request->param('sex'); // 
        $positions = $this->request->param('positions');  // 职务
        $duties = $this->request->param('duties');  //  职责
        $department = $this->request->param('department'); // 部门中
        $tel = $this->request->param('tel'); // 电话 
        $mobile = $this->request->param('mobile'); //个人手机         
        $fax = $this->request->param('fax');  // 传真 
        $email = $this->request->param('email');  // email 个人
        $qq = $this->request->param('qq');  
        //$wechat = $this->request->param('wechat');    
        //$msn = $this->request->param('msn');  
        $link_other = $this->request->param('link_other');  // 其他联系方式         
        $addr = $this->request->param('addr');  // 地址 个人
        // 2
        
        $postalcode = $this->request->param('postalcode');  //邮编  
        //$country = $this->request->param('country');  //国家   
        //$country_code = $this->request->param('country_code');  //国家代码 
        $province = $this->request->param('province');  //省份   
        //$area = $this->request->param('area');  //区域
        $city = $this->request->param('city');  //市   
        $town = $this->request->param('town');  // 区    
     

        // ed
        $data = array('create_uid'=>$create_uid,'remark'=>$remark,'create_time'=>time(),'name'=>$name, 
        'zsid'=>$zsid,'sex'=>$sex,'positions'=>$positions,'duties'=>$duties,
        'department'=>$department,'tel'=>$tel,'mobile'=>$mobile,
        'fax'=>$fax,'email'=>$email,'qq'=>$qq,'link_other'=>$link_other,'addr'=>$addr,'postalcode'=>$postalcode,'province'=>$province,
        'town'=>$town,'city'=>$city     
                      );
       
    
        $r = $this->model->insertData($data);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

       return json(['code'=>1,'msg'=>'操作成功']);
    }

 
    public function editzslinkuser(){
        // com
        // $zsid = $this->request->param('zsid'); // 明细的id 还是展商的id 呢
        $id = $this->request->param('id'); //必须
        $create_uid  = $this->admin['id'];  
        // com 

        //==========
        $remark = $this->request->param('remark');  
        
            
         //== comon    
     
         $name = $this->request->param('name');     // 姓名 
         //$companyid = $this->request->param('zsid');// 企业id  
         $sex = $this->request->param('sex'); // 
         $positions = $this->request->param('positions');  // 职务
         $duties = $this->request->param('duties');  //  职责
         $department = $this->request->param('department'); // 部门中
         $tel = $this->request->param('tel'); // 电话 
         $mobile = $this->request->param('mobile'); //个人手机         
         $fax = $this->request->param('fax');  // 传真 
         $email = $this->request->param('email');  // email 个人
         $qq = $this->request->param('qq');  
         //$wechat = $this->request->param('wechat');    
         //$msn = $this->request->param('msn');  
         $link_other = $this->request->param('link_other');  // 其他联系方式         
         $addr = $this->request->param('addr');  // 地址 个人
         // 2
         
         $postalcode = $this->request->param('postalcode');  //邮编  
         //$country = $this->request->param('country');  //国家   
         //$country_code = $this->request->param('country_code');  //国家代码 
         $province = $this->request->param('province');  //省份   
         //$area = $this->request->param('area');  //区域
         $city = $this->request->param('city');  //市   
         $town = $this->request->param('town');  // 区 
 
      
 
         // ed
         $data = array('remark'=>$remark,'update_time'=>time(),'name'=>$name, 
           'sex'=>$sex,'positions'=>$positions,'duties'=>$duties,
         'department'=>$department,'tel'=>$tel,'mobile'=>$mobile,
         'fax'=>$fax,'email'=>$email,'qq'=>$qq,'link_other'=>$link_other,'addr'=>$addr,'postalcode'=>$postalcode,'province'=>$province,
         'town'=>$town,'city'=>$city     
                       );
        //=======
                      
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误，请核查']);
        }

        // $where['zsid'] = $zsid;
        $where['id'] = $id;
        $r = $this->model->updateBywhere($data,$where);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }


    /**
     * 删除人员
     */
    public function dellinkuser(){
        // $zsid = $this->request->param('zsid');
        $id = $this->request->param('id');     
     
        $where = ['id'=>$id,'is_delete'=>0];
  
        $r = $this->model->countWhere($where);

        if(empty($r)){
            return json(['code'=>0,'msg'=>'记录不存在1，请核实']);
        }
       
        $r = $this->model->deleteWhere(['id'=>$id]);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

     /**
     * 企业管理批量 删除项目
     */
    public function delMoreCompanyfollowlinkuser(){
        $id = $this->request->param('id');
        $create_uid  = $this->admin['id'];           
        $map['id'] = array( 'in',explode(',',$id));    
        $r = $this->model->deleteWhere($map); 
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

        //===导出
        public function export() {
           
            $userid="";
            $type="";
            $usersub ="";
            $cstname ="";
            $seldept = "";
            $sttime ="";
            $edtime =""; 
            //$whereH = array('userid' => '149', 'reportdate' => '20180427');
            // $whereH = array('type' => $type, 'departmy' => $departmy, 'datestar' => $datestar, 'dateend' => $dateend);
            $search_data=array(
                'type' => $type,
                'userid'=>$userid,        
                'usersub'=>$usersub,
                'cstname'=>$cstname,
                'seldept'=>$seldept,
                'sttime'=>$sttime,
                'edtime'=>$edtime
            );
        
            $columns = array(
                //array('title' => '员工ID', 'field' => 'ordersn', 'width' => 24),
                array('title' => '姓名', 'field' => 'realname', 'width' => 12),
                //array('title' => '计划类别', 'field' => 'realname', 'width' => 12),
                array('title' => '部门', 'field' => 'ordersn', 'width' => 24),
                array('title' => '日期', 'field' => 'ordersn', 'width' => 24),
                array('title' => '合同编号', 'field' => 'ordersn', 'width' => 24),
                array('title' => '签约日期', 'field' => 'realname', 'width' => 12),
                array('title' => '客户名称', 'field' => 'mobile', 'width' => 12),
                array('title' => '销售主管', 'field' => 'goods_title', 'width' => 24)
    
            );
            $title = "企业明细";
            $filename = $title.date('Y-m-d-H-i', time()).".xls";      
            $daily=$this->doexport($columns,$filename,$search_data);
            exit;
    
        }
     
        //common xsport
        public function doexport($datas,$filename,$sort){
            $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";
    
            $str .="<table border=1>";
            $condition="";
              if($sort['usersub'] !=""){
                $condition.=" and d.username='".$sort['usersub']."' ";
              } 
              if($sort['cstname'] !=""){
                  $condition.=" and coustname like'%".$sort['cstname']."%' ";
              }
              if($sort['sttime'] !=""){
                $condition.=" and (DATE_FORMAT(d.dateSign,'%Y/%m/%d')>='".$sort['sttime']."' and DATE_FORMAT(d.dateSign,'%Y/%m/%d') <='".$sort['edtime']."') ";
              } 
             
            // $sql="select  d.*,u.dept from wp_wsmanage_salecontract d
            //     inner join wp_user u on u.id=d.userid
            //     where d.id !='' $condition order by d.id desc ";

             $list=$this->companyListall();
    
            // var_dump($datas);die; 
            $tht = "<tr>";//biao tr
            foreach($datas as $key =>$trno){
                $tht .= "<th>{$trno['title']}</th>";
            }
            $tht .= "</tr>";
            $str .= $tht;
            foreach ($list as $key){
                
                $str .= "<tr>";
                $str .= "<td>$key->company_name</td>";
                if($key->company_name=="18"){
                    $str .= "<td>销售一部</td>";
                }             
                else{
                    $str .= "<td>2222</td>";
                }
    
                $str .= "<td>$key->company_name_en</td>";
                $str .= "<td>$key->company_describe</td>";
                $str .= "<td>$key->tel</td>";
                $str .= "<td>$key->product</td>";
                $str .= "<td>$key->product_brand</td>";
                $str .= "</tr>";
    
            }
    
            $str .= "</table></body></html>";
            header( "Content-Type: application/vnd.ms-excel; name='excel'" );   #类型
            header( "Content-type: application/octet-stream" );
            header( "Content-Disposition: attachment; filename=".$filename );
            header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
            header( "Pragma: no-cache" );
            header( "Expires: 0" );
            exit( $str );
        }   

    // 导入 
    
    // $file
    public function import5(){
        //$file是上传到服务器后的excel文件路径
        $path=ROOT_PATH.'public/static/test.xls';
        $file=$path;
        //判断excel文件格式
        $type = pathinfo($file);
       
        $type = strtolower($type["extension"]);
        if ($type=='xlsx') {
            $type='Excel2007';
        }elseif($type=='xls') {
            $type = 'Excel5';
        }
        $objReader =\PHPExcel_IOFactory::createReader($type);
        $objPHPExcel = $objReader->load($file, $encode = 'utf-8');
        $excel_array = $objPHPExcel->getsheet(0)->toArray();
        $res = [];
        array_shift($excel_array);//去掉第一行，因为我上传的excel文件第一行为列名
        if(!is_array($excel_array) || empty($excel_array)){
          return $res;
        }else{
          //循环读取数据
          foreach ($excel_array as $key => $value) {
            $res[$key]['msn'] = (string)$value[0];
            $res[$key]['wechat'] = (string)$value[6];
            $res[$key]['company_name'] = (string)$value[8];
          }
        }   
        $this->dodaorudata($res);

    }
    // 执行到数据库
    public function dodaorudata($res){

    
        foreach($res as $item){

            $r = $this->model->insertData($item);
            if(!$r){
                return json(['code'=>0,'msg'=>'操作失败，请重试']);
            }
    
          
        }

        return json(['code'=>1,'msg'=>'操作成功']);

    }
    //
 

   
   
   
      // ces

    public function test1(){ 
        //==common
        $create_uid  = $this->admin['id'];
        //
        $company_name = 11; //公司中文名
      
        $company_name_en =2; //    
        $company_describe = 3; //简介             

        $data = array('create_time'=>time(),'company_name'=>$company_name,
        'company_name_en'=>$company_name_en,'company_describe'=>$company_describe           
    
         );
       
        // var_dump ($data);die;
        $r = $this->model->insertData($data);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

       return json(['code'=>1,'msg'=>'操作成功']);
    }


     
      // ces2

    public function test2(){ 
        //==common
        $create_uid  = $this->admin['id'];
        //
        $company_name = 11; //公司中文名
      
        $company_name_en =2; //    
        $company_describe = 3; //简介             

        $data = array('create_time'=>time(),'company_name'=>$company_name,
        'company_name_en'=>$company_name_en,'company_describe'=>$company_describe           
    
         );
       
        // var_dump ($data);die;
        // $r = $this->model->selt1($data);
        $r = $this->model->inset1($data);
        var_dump ($r);die;
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

       return json(['code'=>1,'msg'=>'操作成功']);
    }


    //ces end


}